package com.security.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * @Author zhang
 * @Create 2021/7/13 10:15
 */
public class StringUtils {

    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    public static boolean isEmpty(String str) {
        if (str == null || str.isEmpty()) return true;
        str = str.trim();
        if (str.isEmpty()) return true;
        return str.equalsIgnoreCase("null");
    }

    /**
     * 处理截取字符串
     */
    public static String getStringByHexMsg(String msg, int beginIndex, int endIndex) {
        if (isEmpty(msg)) {
            return "";
        }
        if (msg.length() >= endIndex * 2) {
            return msg.substring(beginIndex * 2, endIndex * 2);
        }
        return "";
    }

    /**
     * 处理截取字符串
     */
    public static String getStringByHexMsg(String msg, int beginIndex) {
        if (isEmpty(msg)) {
            return "";
        }
        if (msg.length() >= beginIndex * 2) {
            return msg.substring(beginIndex * 2);
        }
        return "";
    }


    //length用户要求产生纯数字字符串的长度
    public static String getRandomNumberStr(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(10);
            sb.append(number);
        }
        return sb.toString();
    }

    /**
     * 获取ip
     *
     * @param request 请求体
     * @return 结果
     */
    public static String getIPAddress(HttpServletRequest request) {
        String ip = null;
        //X-Forwarded-For：Squid 服务代理
        String ipAddresses = request.getHeader("X-Forwarded-For");
        if (ipAddresses == null || ipAddresses.isEmpty() || "unknown".equalsIgnoreCase(ipAddresses)) {
            //Proxy-Client-IP：apache 服务代理
            ipAddresses = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddresses == null || ipAddresses.isEmpty() || "unknown".equalsIgnoreCase(ipAddresses)) {
            //WL-Proxy-Client-IP：weblogic 服务代理
            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddresses == null || ipAddresses.isEmpty() || "unknown".equalsIgnoreCase(ipAddresses)) {
            //HTTP_CLIENT_IP：有些代理服务器
            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddresses == null || ipAddresses.isEmpty() || "unknown".equalsIgnoreCase(ipAddresses)) {
            //X-Real-IP：nginx服务代理
            ipAddresses = request.getHeader("X-Real-IP");
        }
        //有些网络通过多层代理，那么获取到的ip就会有多个，一般都是通过逗号（,）分割开来，并且第一个ip为客户端的真实IP
        if (ipAddresses != null && !ipAddresses.isEmpty()) {
            ip = ipAddresses.split(",")[0];
        }
        //还是不能获取到，最后再通过request.getRemoteAddr();获取
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ipAddresses)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }


}
